package com.lancoo.answer.view.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.lancoo.answer.R
import com.lancoo.answer.model.bean.ModelAnswerArea
import com.lancoo.answer.model.bean.TextInfo
import com.lancoo.answer.util.DpUtils
import com.lancoo.answer.util.ExamFontSizeAdjustUtils
import com.lancoo.answer.util.HtmlUtils


/**
 * Created by wealive on 2021/9/15.
 *
 *
 */
class CorrectMistakesTextAdapter(
    val type: Int,
    val context: Context,
    val list: List<TextInfo>,
    val area: MutableList<ModelAnswerArea>
) :
    FlowAdapter<TextInfo>(list) {
    @SuppressLint("SetTextI18n")
    override fun getView(position: Int): View {
        val textInfo = list[position]
        val view: View =
            LayoutInflater.from(context)
                .inflate(R.layout.ev_item_correct_mistakes_text, null, false)
        if (type == 0) {
            view.setPadding(0, DpUtils.dip2px(context, 12f), 0, DpUtils.dip2px(context, 12f))
        } else {
            view.setPadding(0, DpUtils.dip2px(context, 18f), 0, DpUtils.dip2px(context, 18f))
        }
        val tv = view.findViewById<TextView>(R.id.tv_text)
        if (area.size > 1) {
            area.forEach {
                val startArea = it.AnswerArea!!.split("-")[0].toInt()
                val endArea = it.AnswerArea!!.split("-")[1].toInt()
                if (list[position].Index in startArea..endArea) {
                    tv.typeface = Typeface.createFromAsset(context.assets, "fonts/TimesNew.ttf")
                    if (list[position].Index == startArea && area.size == 10) {
                        val param = tv.layoutParams as ConstraintLayout.LayoutParams
                        param.marginStart = DpUtils.dip2px(context, 16f)
                    }
                }
            }
        } else {
            tv.typeface = Typeface.createFromAsset(context.assets, "fonts/TimesNew.ttf")
        }
        tv.text = if (textInfo.Index != -1) HtmlUtils.setText(textInfo.Text) else ""
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(tv,14)
        return view
    }


}